#!/usr/bin/env python
# -*- coding: utf-8 -*-
###################################################################
# Author: WangDonghao
# Date  : 2020.12
###################################################################

from dayu_widgets.qt import *
import os


class SavingHinterWin(QDialog):

    def __init__(self, parent):
        super(SavingHinterWin, self).__init__(parent=parent)
        self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint|Qt.Tool)
        lay = QVBoxLayout()
        lay.setContentsMargins(0, 0, 0, 0)
        self.setLayout(lay)
        self.label = QLabel()
        self.label.setMaximumHeight(50)
        self.label.setMaximumWidth(300)
        self.label.setScaledContents(True)
        self.label.setAttribute(Qt.WA_TranslucentBackground)
        self.setAttribute(Qt.WA_TranslucentBackground)
        lay.addWidget(self.label)
        self.setMaximumWidth(200)
        self.setContextMenuPolicy(Qt.CustomContextMenu)
        self.customContextMenuRequested.connect(self.show_menu)
        self.contextMenu = QMenu(self)
        action = self.contextMenu.addAction(u'关闭')
        action.triggered.connect(self.close)

        self.set_image_timer = QTimer()
        self.set_image_timer.timeout.connect(self.set_img)
        self.set_image_timer.start(50)
        self.current_img = 0

        self.q_settings = QSettings(u'ANIME_WOK_CONFIG', 'SaveHinter')

        self.get_settings()

    def get_settings(self):
        self.hinter_cfg = self.q_settings.value('interval')
        if not self.hinter_cfg:
            self.save_hinter_timer = QTimer()
            self.save_hinter_timer.timeout.connect(self.show)
            self.save_hinter_timer.start(1800000)
            try:
                print u'未设置保存提醒间隔，将自动设置为30分钟一次.'
            except:
                pass
            return
        if self.hinter_cfg == 'off':
            try:
                print u'保存提醒已关闭'
            except:
                pass
            return
        else:
            self.save_hinter_timer = QTimer()
            self.save_hinter_timer.timeout.connect(self.show)
            self.save_hinter_timer.start(int(self.hinter_cfg)*60*1000)
            try:
                print u'保存提醒已被设置为%s分钟一次'% (int(self.hinter_cfg))
            except:
                pass

    def show(self):
        super(SavingHinterWin, self).show()
        hinter_cfg = self.q_settings.value('interval')
        if not hinter_cfg == self.hinter_cfg:
            self.get_settings()
            print u'保存提醒配置已更改'
            return
        self._desktop = QApplication.instance().desktop()
        self.startPos = QPoint(self._desktop.screenGeometry().width()*2, 50)
        self.endPos = QPoint(-1000, 50)
        self.move(self.startPos)
        self.initAnimation(self.startPos, self.endPos)

    def initAnimation(self, startPos, endPos):
        # 往上移动动画
        moveAnimation = QPropertyAnimation(self, "pos")
        moveAnimation.setStartValue(startPos)
        moveAnimation.setEndValue(endPos)
        moveAnimation.setEasingCurve(QEasingCurve.Linear)
        moveAnimation.setDuration(30000)  # 在5秒的时间内完成
        self.animationGroup = QParallelAnimationGroup(self)
        self.animationGroup.addAnimation(moveAnimation)
        self.animationGroup.start()
        self.animationGroup.finished.connect(self.close)
        
    def enterEvent(self, *args, **kwargs):
        self.animationGroup.pause()

    def leaveEvent(self, *args, **kwargs):
        self.animationGroup.start()

    def show_menu(self, *args):
        self.contextMenu.popup(QCursor.pos())
        self.contextMenu.show()

    def set_img(self):
        self.current_img = (self.current_img % 12) + 1
        current = str(self.current_img).zfill(2)
        self.label.setPixmap(QPixmap(os.environ['WOKWOK_ROOT'] + "/resources/movies/colorful-cat/%s.png" % current))
        # self.label.setPixmap(QPixmap("D:/dango_repo/WOKWOK/resources/movies/colorful-cat/%s.png" % current))


if __name__ == '__main__':
    import sys
    from dayu_widgets.qt import QApplication

    app = QApplication(sys.argv)
    test = SavingHinterWin(None)
    from dayu_widgets.theme import MTheme
    light = MTheme('light')
    light.apply(test)
    test.show()
    sys.exit(app.exec_())